package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年09月03日16:25
 */

/**
 * 该servlet用于实现用户登录功能
 *当用户登录的时候判断用户输入的用户名与密码是否与数据库中的一致
 * 如果一致
 *      表示登录成功，将正确的username和password保存到cookie中去
*  如果不一致
 *      说明输入的用户名和密码不正确
 *      页面仍然留在login.jsp
 *      且页面展示错误消息
 */

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户输入数据
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection(url,"root","root");
            String sql=new StringBuffer()
                    .append(" select id,username,password ")
                    .append(" from t_user ")
                    .append(" where username=?  ")
                    .append(" and password=? ")
                    .toString();

            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if(rs.next()){
                // 表示登录成功，将正确的username和password保存到cookie中去
                CookieUtil.addCookie("username",username,86400,response);
                CookieUtil.addCookie("password",password,86400,response);
                response.sendRedirect(request.getContextPath()+"/jsp/main.jsp");
                // request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);
                return;
            }
            request.setAttribute("msg","用户名或者密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);


        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
